import java.awt.*;

/**
 * Created by obila_000 on 2014/10/24.
 */
public class Flipper extends Gizmos{
    enum Flipper_type{
        LEFT,
        RIGHT
    }
    private float width;
    private float height;
    private float boundingBoxHeight;
    private float boundingBoxWidth;
    private float rotationSpeed = 1.0f;
    private float rotateAngle = 0;
    private final static float MAX_ROTATION_ANGLE = 90.0f;
    private boolean isRotating = false;


    private Color color = new Color(231, 97,87);

    // Keep track of the animation window that will be drawing this ball.
    private AnimationWindow win;

    /**
     * Constructor.
     * @param win Animation window that will be drawing this ball.
     */
    public Flipper(AnimationWindow win) {
        super(win);
        this.win = win;
        this.x = 0;
        this.y = 0;
        this.height = 10;
        this.width = 35*2;
        this.boundingBoxHeight = 35*2;
        this.boundingBoxWidth = 35*2;
        this.setType(Gizmos_type.FLIPPER_LEFT);
    }

    /**
     * Constructor.
     * 注：此图形的定位点不在中心
     * @param win Animation window that will be drawing this ball.
     */
    public Flipper(AnimationWindow win, float x, float y, float width, float height, Flipper_type type) {
        super(win);
        this.win = win;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.boundingBoxWidth = width;
        this.boundingBoxHeight = width;
        if(type == Flipper_type.LEFT)
            this.setType(Gizmos_type.FLIPPER_LEFT);
        if(type == Flipper_type.RIGHT)
            this.setType(Gizmos_type.FLIPPER_RIGHT);
    }

//    public void setPosition(float x, float y){
//        this.x = x;
//        this.y = y;
//    }
//
//    public Point getPostion(){
//        return new Point((int)(x),(int)(y));
//    }
    /**
     * @modifies the Graphics object <g>.
     * @effects paints a circle on <g> reflecting the current position
     * of the ball.
     * @param g Graphics context to be used for drawing.
     */
    @Override public void paint(Graphics g) {

        // the "clip rectangle" is the area of the screen that needs to be
        // modified
        Rectangle clipRect = g.getClipBounds();

        // For this tiny program, testing whether we need to redraw is
        // kind of silly.  But when there are lots of objects all over the
        // screen this is a very important performance optimization
        if (clipRect.intersects(this.boundingBox())) {
            g.setColor(color);
            Graphics2D graphics2D = (Graphics2D)g;
            AlphaComposite ac2 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.getPellucidity());
            graphics2D.setComposite(ac2);
            int topX[] = {(int)(x - height/2),(int)(x - height/2),(int)(x + height/2),(int)(x + height/2)};
            int topY[] = {(int)(y - height/2),(int)(y + width - height/2),(int)(y + width - height/2),(int)(y - height/2)};
            //旋转
            //action();
            int[][] newTops = Physics.afterRotate(topX,topY,rotateAngle,(int)x,(int)y);
            if(isRotating) {
                this.setShape(new Polygon(newTops[0], newTops[1], 4));
                g.fillPolygon(newTops[0], newTops[1], 4);
            }
            else {
                this.setShape(new Polygon(topX, topY, 4));
                g.fillPolygon(topX, topY, 4);
            }
            //恢复透明度
            AlphaComposite oringe = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
            graphics2D.setComposite(oringe);
        }
    }

    /**
     * @return the smallest rectangle that completely covers the current
     * position of the ball.
     */
    @Override public Rectangle boundingBox() {

        // a Rectangle is the x,y for the upper left corner and then the
        // width and height
        return new Rectangle((int)(x - width/2), (int)(y - height/2), (int)(width),
                (int)(height));
    }

    @Override public void judgeCollispeWithBall(BouncingBall ball){
        Point ballPos = ball.getBallCenterPos();
        float ballRadius = ball.getRadius();
        if(this.getShape()!=null&&this.getShape().contains(ballPos)){
            ball.randomBump();
        }
    }

//    @Override public boolean inGizmos(Point point){
//        if(this.getShape().contains(point)){
//            return true;
//        }
//        return false;
//    }

    @Override public void startFocusAnimation(){
        setPellucidity(0.2f);
    }

    @Override public void startLeaveAnimation(){
        setPellucidity(1.0f);
    }

    @Override public void action(){
        if(doAction) {
            switch (getType()) {
                case FLIPPER_RIGHT:
                    if (rotateAngle >= -MAX_ROTATION_ANGLE) {
                        rotateAngle -= rotationSpeed;
                        isRotating = true;
                    } else {
                        rotateAngle = 0;
                        isRotating = false;
                    }
                    break;
                case FLIPPER_LEFT:
                    if (rotateAngle <= MAX_ROTATION_ANGLE) {
                        rotateAngle += rotationSpeed;
                        isRotating = true;
                    } else {
                        rotateAngle = 0;
                        isRotating = false;
                    }
                    break;
            }
        }
    }
}
